package day004;


/**
 * 2数之和
 */
public class TwoSum {
    public static void main(String[] args) {
        twoSum(new int[]{2, 7, 11, 15}, 13);
    }

    static int[] twoSum(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            int sum = nums[left] + nums[right];
            if (sum == target) {
                return new int[]{left + 1, right + 1};
            } else if (sum < target) {
                left++;
            } else if (sum > target) {
                right--;
            }
        }
        return new int[]{-1, -1};
    }
}
